package com.glorysoft.knowlespda.ui.eqpauthor.viewmodel

import androidx.databinding.ObservableArrayList
import com.glorysoft.knowlespda.BR
import com.glorysoft.knowlespda.R
import com.glorysoft.knowlespda.base.BaseViewModel
import com.glorysoft.knowlespda.base.Constant
import com.glorysoft.knowlespda.entity.AuthEntity
import com.glorysoft.knowlespda.entity.PairingEntity
import com.glorysoft.knowlespda.model.PairRepository
import com.google.gson.Gson
import com.google.gson.JsonObject
import com.google.gson.JsonParser
import me.tatarka.bindingcollectionadapter2.ItemBinding

/**
 * FileName: EqpAuthorizationViewModel
 * Author: XJH
 * Date: 2022/7/13 10:08
 * Description:
 * History:
 */
class EqpAuthorizationViewModel : BaseViewModel() {

    val pairRepository by lazy { PairRepository() }

    var authItems = ObservableArrayList<AuthEntity.PdaEntity>()
    var authBinding =
        ItemBinding.of<AuthEntity.PdaEntity>(BR.pdaItem, R.layout.item_auth)

    fun getPdaAuthInfo(){
        val map = JsonObject()
        map.addProperty("OPID",Constant.CURRENT_USER)
        map.addProperty("PDANO",Constant.PDA_NO)
        launchOnlyResult({
            val pdaAuthInfo = pairRepository.getPdaAuthInfo(map)
            authItems.addAll(pdaAuthInfo.pdanos)
        })
    }

    fun updateAuth(){
        val map = JsonObject()
        map.addProperty("OPID",Constant.CURRENT_USER)
        map.addProperty("PDANO",Constant.PDA_NO)
        map.add("PDANOS", JsonParser().parse(Gson().toJson(authItems)).asJsonArray) //传批次集合
        launchOnlyResult({
            pairRepository.updatePdaAuthInfo(map)
            defUI.startActivity.call()
        })
    }
}